package com.dingreading.cloud.admin.service.impl;

import com.dingreading.cloud.admin.dto.StoreProjectCycleDto;
import com.dingreading.cloud.admin.entity.StoreProjectCycle;
import com.dingreading.cloud.admin.entity.table.StoreProjectCycleTableDef;
import com.dingreading.cloud.admin.mapper.StoreProjectCycleMapper;
import com.dingreading.cloud.admin.service.StoreProjectCycleService;
import com.dingreading.cloud.common.util.NepUtil;
import com.dingreading.cloud.common.util.PageUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryMethods;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.update.UpdateChain;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 服务层实现。
 *
 * @author Administrator
 * @since 2023-11-22
 */
@Service
public class StoreProjectCycleServiceImpl extends BaseServiceImpl<StoreProjectCycleMapper, StoreProjectCycle, StoreProjectCycleTableDef> implements StoreProjectCycleService {

    @Override
    protected StoreProjectCycleTableDef getTable() {
        return StoreProjectCycleTableDef.storeProjectCycle;
    }

    @Override
    public Page<StoreProjectCycle> pageList(PageUtil pageUtil, String keyword, Integer enabled) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.storeUid.isNull())
                .and(table.projectUid.isNull());
        if (StringUtils.isNotBlank(keyword))
            condition.and(table.cycleName.like(keyword));
        if (null != enabled)
            condition.and(table.enabled.eq(enabled));

        QueryWrapper wrapper = QueryWrapper.create()
                .where(condition)
                .orderBy(table.orderNum.asc());

        return getMapper().paginate(Page.of(pageUtil.getPage(), pageUtil.getRows()), wrapper);
    }

    @Override
    public boolean haveName(String uid, String projectUid, String name) {
        QueryCondition condition = QueryCondition.createEmpty();
        if (StringUtils.isNotBlank(uid))
            condition.and(table.uid.ne(uid));
        condition.and(table.cycleName.eq(name));
        if (StringUtils.isNotBlank(projectUid))
            condition.and(table.projectUid.eq(projectUid));
        else
            condition.and(table.projectUid.isNull());

        return exists(condition);
    }

    @Override
    public boolean updateEnabled(String uid, Integer enabled, String staffUid, String staffName) {
        return UpdateChain.of(StoreProjectCycle.class)
                .set(table.enabled, enabled)
                .set(table.lastStaffUid, NepUtil.nullToZero(staffUid))
                .set(table.lastStaffName, NepUtil.nullToZero(staffName))
                .where(table.uid.eq(uid))
                .update();
    }

    @Override
    public int dropBean(String uid) {
        return getMapper().deleteByCondition(table.uid.eq(uid));
    }

    @Override
    public int getMaxShowOrder(String projectUid) {
        QueryCondition condition = QueryCondition.createEmpty();
        if (StringUtils.isNotBlank(projectUid))
            condition.and(table.projectUid.eq(projectUid));
        else
            condition.and(table.projectUid.isNull());


        QueryWrapper wrapper = QueryWrapper.create()
                .select(QueryMethods.max(table.orderNum))
                .from(table)
                .where(condition);

        Integer integer = getMapper().selectObjectByQueryAs(wrapper, Integer.class);
        return NepUtil.nullToZero(integer);
    }

    @Override
    public StoreProjectCycle getGtOrderNum(String projectUid, Integer orderNum) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.orderNum.gt(orderNum));
        if (StringUtils.isNotBlank(projectUid))
            condition.and(table.projectUid.eq(projectUid));
        else
            condition.and(table.projectUid.isNull());

        QueryWrapper wrapper = QueryWrapper.create()
                .where(condition)
                .orderBy(table.orderNum.asc());

        return getMapper().selectOneByQuery(wrapper);
    }

    @Override
    public StoreProjectCycle getLtOrderNum(String projectUid, Integer orderNum) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.orderNum.lt(orderNum));
        if (StringUtils.isNotBlank(projectUid))
            condition.and(table.projectUid.eq(projectUid));
        else
            condition.and(table.projectUid.isNull());

        QueryWrapper wrapper = QueryWrapper.create()
                .where(condition)
                .orderBy(table.orderNum.desc());

        return getMapper().selectOneByQuery(wrapper);
    }

    @Override
    public boolean updateOrderNum(String uid, Integer orderNum, String staffUid, String staffName) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.uid.eq(uid));
        return UpdateChain.of(StoreProjectCycle.class)
                .set(table.orderNum, orderNum)
                .set(table.lastStaffUid, NepUtil.nullToZero(staffUid))
                .set(table.lastStaffName, NepUtil.nullToZero(staffName))
                .where(condition)
                .update();
    }

    @Override
    public StoreProjectCycle getByUid(String uid) {
        return getMapper().selectOneByCondition(table.uid.eq(uid));
    }

    @Override
    public List<StoreProjectCycleDto> listByProjectUidAndSourceAndEnabled(
            String projectUid, Integer source, Integer enabled
    ) {
        QueryCondition condition = table.projectUid.eq(projectUid);
        if (source != null)
            condition.and(table.source.eq(source));
        if (enabled != null)
            condition.and(table.enabled.eq(enabled));

        QueryWrapper wrapper = QueryWrapper.create()
                .where(condition)
                .orderBy(table.orderNum.asc());
        return getMapper().selectListByQueryAs(wrapper, StoreProjectCycleDto.class);
    }

    @Override
    public boolean updateInfo(
            String uid, String cycleName, Integer days, Integer lessons, Double totalAmount, Double unitAmount,
            String staffUid, String staffName
    ) {
        QueryCondition condition = table.uid.eq(uid);
        return UpdateChain.of(StoreProjectCycle.class)
                .set(table.cycleName, cycleName)
                .set(table.days, days)
                .set(table.lessons, lessons)
                .set(table.totalAmount, totalAmount)
                .set(table.unitAmount, unitAmount)
                .set(table.lastStaffUid, NepUtil.nullToZero(staffUid))
                .set(table.lastStaffName, NepUtil.nullToZero(staffName))
                .where(condition)
                .update();
    }

}
